package com.baiyun.bs.controller.student;


import com.baiyun.bs.base.BaseApiController;
import com.baiyun.bs.base.RestResponse;
import com.baiyun.bs.domain.Message;
import com.baiyun.bs.domain.MessageUser;
import com.baiyun.bs.domain.Notice;
import com.baiyun.bs.domain.User;
import com.baiyun.bs.service.MessageService;
import com.baiyun.bs.service.NoticeService;
import com.baiyun.bs.service.UserService;
import com.baiyun.bs.utility.DateTimeUtil;
import com.baiyun.bs.utility.PageInfoHelper;
import com.baiyun.bs.viewmodel.admin.message.MessagePageRequestVM;
import com.baiyun.bs.viewmodel.admin.message.MessageResponseVM;
import com.baiyun.bs.viewmodel.admin.message.MessageSendVM;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController("StudentMessageController")
@RequestMapping(value = "/api/student/message")
public class MessageController extends BaseApiController {

    @Autowired
    private NoticeService noticeService;


    @RequestMapping(value = "/getAllNotice", method = RequestMethod.POST)
    public RestResponse<List<Notice>> getAllNotice(@RequestBody Notice notice) {
        List<Notice> allNotices = noticeService.getAllNotices(notice );
        return RestResponse.ok(allNotices);
    }
    @RequestMapping(value = "/selectNotice/{nId}", method = RequestMethod.POST)
    public RestResponse<Notice> selectNoticeById(@PathVariable Long nId) {
        Notice notice = noticeService.selectNoticeById(nId);
        return RestResponse.ok(notice);
    }
    @RequestMapping(value = "/addNotice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse<Void> addNotice(@RequestBody Notice notice) {
        if (notice.getnId() != null) {
            noticeService.updateNotice(notice);
        }else {
            noticeService.addNotice(notice);
        }
        return RestResponse.ok();
    }

    @RequestMapping(value = "/updateNotice/{nId}" , method = RequestMethod.POST)
    public RestResponse<Void> updateNotice(@PathVariable Long nId, @RequestBody Notice notice) {
        notice.setnId(nId);
        noticeService.updateNotice(notice);
        return RestResponse.ok();
    }


    @RequestMapping(value = "/deleteNotice/{nId}", method = RequestMethod.POST)
    public RestResponse<Void> deleteNotice(@PathVariable Long nId) {
        try {
            noticeService.deleteNotice(nId);
            return RestResponse.ok();
        } catch (Exception e) {
            // 记录异常信息到日志文件
            return RestResponse.fail(500, "系统内部错误，删除通知失败");
        }
    }



}
